//
//  JCVistorView.swift
//  swiftWeibo
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

protocol JCVistorViewDelegate :NSObjectProtocol{

    // 注册按钮被点击
    func onClickVisitorViewRegister()
    
    // 登陆按钮被点击
    func onClickvisitorViewLogin()
    
}


class JCVistorView: UIView {
    
    weak var delegate : JCVistorViewDelegate?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化
        // 设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        // 创建子控件
        setupSubViews()
        // 创建约束
        updateConstraints()
    }

    func setupVisitorInfo(imgName: String, message: String){
        
        iconView.image = UIImage(named: imgName)
        messageLabel.text = message
        homeView.hidden = true;
        coverView.hidden = true;
    }
    
    func startRotationAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        var pauseTime = iconView.layer.timeOffset
        if pauseTime == 0{
            pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        }
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    
    func setupSubViews(){
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    }
    
    override func updateConstraints() {
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.center.equalTo(self)
            make.centerY.equalTo(self).offset(-40)
        }
        
        homeView.snp_updateConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_updateConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(226)
        }
        
        registerButton.snp_updateConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(5)
            make.width.equalTo(90)
            make.height.equalTo(35)
        }
        
        loginButton.snp_updateConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.width.height.equalTo(registerButton)
        }
        coverView.snp_updateConstraints { (make) -> Void in
            make.top.right.left.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        super.updateConstraints()
    }
    
    // MARK: - 代理
    @objc private func onClickRegister(){
    
        // 注册按钮被点击
       delegate?.onClickVisitorViewRegister()
        
    }
    @objc private func onClickLogin(){
        
        delegate?.onClickvisitorViewLogin()
        
    }
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        // 设置label文本了
        label.text = "关注一些人,看看有神马惊喜"
        label.numberOfLines = 0
        
        // 根据文本来适应大小
        label.sizeToFit()
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "onClickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        
        // 设置文本颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 根据内容适应大小
        button.sizeToFit()
        
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "onClickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        // 设置文本颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 根据内容适应大小
        button.sizeToFit()
        
        // 添加点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 遮罩视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    func loginClick(){
    
        
        
    }
}
